<?php

namespace App\Admin\Controllers;

use App\Actions\ExhibitorInformationBatchTransfer;
use App\Alliance\Actions\Post\CustomerBatchTransfer;
use App\Models\JournalContent;
use App\Models\MainBusiness;
use App\Models\Unit;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\CsvExporter;
use Illuminate\Support\Str;

class JournalContentController extends AdminController
{
    protected $model = JournalContent::class;

    protected function title()
    {
        return '会刊内容';
    }

    protected function grid()
    {
        $grid = new Grid(new $this->model);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter(); //去掉默认的id过滤器

            $main_business_array = MainBusiness::query()->where('status', 1)->pluck('name', 'id')->toArray();
            $filter->like('exhibitor_information.name', trans('admin.keywords'));
            $filter->like('exhibitor_information.sales_personnelcompany.personnel_companies', trans('exhibitor_information.sales'));
            $filter->where(function ($query) {
                $main_business_id_array = $this->input;
                $main_business_ids = [];
                foreach ($main_business_id_array as $main_business_id) {
                    $main_business_id = JournalContent::query()->where('main_business', 'like', "%{$main_business_id}%")->pluck('id')->toArray();
                    // 合并数组
                    $main_business_ids = array_merge($main_business_ids, $main_business_id);

                }

                $main_business_ids = array_unique($main_business_ids);

                $query->whereIn('id', $main_business_ids);
            }, trans('journal_content.main_business'))->checkbox($main_business_array);
        });

        //$grid->disableExport();

        $grid->model()->orderByDesc('id');
        $grid->column('exhibitor_information.name', trans('journal_content.exhibitor_information_id'));
        $grid->column('exhibitor_information.booth_number', trans('exhibitor_information.booth_number'));
        $grid->column('en_enterprise_full', trans('exhibitor_information.en_name'));
//        $grid->column('cn_enterprise_full', trans('journal_content.cn_enterprise_full'));
//        $grid->column('en_enterprise_full', trans('journal_content.en_enterprise_full'));
//        $grid->column('telephone', trans('journal_content.telephone'));
//        $grid->column('e_mail', trans('journal_content.e_mail'));
        $grid->column('cn_address', trans('journal_content.cn_address'));
        $grid->column('en_address', trans('journal_content.en_address'));

        $grid->column('post_code', trans('journal_content.post_code'));
        $grid->column('telephone', trans('journal_content.telephone'));
//        $grid->column('wx', trans('journal_content.wx'));
        $grid->column('e_mail', trans('journal_content.e_mail'));
        $grid->column('cn_company_introduction', trans('journal_content.cn_company_introduction'))->display(function ($cn_company_introduction) {

            $cn_company_introduction_str = Str::limit(strip_tags($cn_company_introduction), 10 * 2, '...');//保留18个字
            return "<span title='{$cn_company_introduction}'>{$cn_company_introduction_str}</span>";
        });
        $grid->column('en_company_introduction', trans('journal_content.en_company_introduction'))->display(function ($en_company_introduction) {
            $en_company_introduction_str = Str::limit(strip_tags($en_company_introduction), 10 * 2, '...');//保留18个字
            return "<span title='{$en_company_introduction}'>{$en_company_introduction_str}</span>";
        });
        $grid->column('url', trans('journal_content.url'))
            ->display(function () {
                $url = $this->url;
                if ($url) {
                    return "<a href='{$url}' target='_blank'>点击跳转</a>";
                }

            });
        $grid->column('files', trans('journal_content.files'))->display(function () {
            $files = $this->files;

            if ($files) {
                if (strpos($files, 'http') !== false) {
                    return "<a href='{$files}' target='_blank'>logg下载</a>";
                } else {
                    $domain = env('QINIU_DOMAIN');
                    return "<a href='{$domain}/{$files}' target='_blank'>logg下载</a>";
                }

            }

        });
        $grid->column('main_business', trans('journal_content.main_business'))->display(function ($main_business) {
            $main_business_arr = explode(',', $main_business);

            $main_business_array = [];
            foreach ($main_business_arr as $key => $value) {
                $main_business = MainBusiness::query()->find($value);
                $main_business_array[] = $main_business->name;
            }
            return implode(',', $main_business_array);

        });
        $grid->column('other', trans('journal_content.other'));
        $grid->column('created_at', trans('journal_content.created_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $grid->export(function ($export) {
            $export->filename('会刊内容');
            $export->column('files', function ($value, $original) {
                return $original;
            });
            $export->column('url', function ($value, $original) {
                return $original;
            });


            $export->column('cn_company_introduction', function ($value, $original) {
                return $original. "\t";//导出字符串格式

            });
            $export->column('en_company_introduction', function ($value, $original) {
                return $original. "\t";//导出字符串格式

            });
        });
        return $grid;
    }


    protected function form()
    {
        $form = new Form(new $this->model);

        $form->text('cn_enterprise_full', trans('journal_content.cn_enterprise_full'));
        $form->text('en_enterprise_full', trans('journal_content.en_enterprise_full'));
        $form->text('cn_address', trans('journal_content.cn_address'));
        $form->text('en_address', trans('journal_content.en_address'));
        $form->text('post_code', trans('journal_content.post_code'))->help('如您提供国外地址，请将邮编写在地址的最后');
        $form->text('telephone', trans('journal_content.telephone'))->help('参照样式：86-10-87654321');
        $form->text('e_mail', trans('journal_content.e_mail'));
        $form->text('url', trans('journal_content.url'));
        $form->file('files', trans('journal_content.files'))->removable()->help('文件格式jpg，且不能超过5MB');
        $form->textarea('cn_company_introduction', trans('journal_content.cn_company_introduction'))->attribute('maxlength', 200);
        $form->textarea('en_company_introduction', trans('journal_content.en_company_introduction'))->attribute('maxlength', 200);
        // 通过闭包设置options
        $form->checkbox('main_business', trans('journal_content.main_business'))->options(function () {
            return MainBusiness::query()->where('status', 1)->pluck('name', 'id');
        })->rules('required');
        $form->text('other', trans('journal_content.other'));

        $form->tools(function ($tools) {
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
        });

        return $form;
    }

}
